<template>
  <div>
    <el-table :data="tableData" stripe style="width: 100%" max-height="600">
      <el-table-column
        prop="orderBy"
        label="排序"
        width="60"
        v-if="$route.meta.type == 1"
      >
      </el-table-column>
      <el-table-column prop="title" label="标题" show-overflow-tooltip>
      </el-table-column>
      <el-table-column
        prop="author"
        label="作者"
        width="100"
        v-if="$route.meta.type == 1"
      >
      </el-table-column>
      <el-table-column
        prop="isShow"
        label="发布状态"
        width="100"
        v-if="$route.meta.type == 1"
      >
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.isShow"
            active-value="0"
            inactive-value="1"
            @change="handleStatus(scope.row)"
          >
          </el-switch>
        </template>
      </el-table-column>

      <el-table-column
        prop="pudDate"
        label="发布时间"
        width="180"
        v-if="$route.meta.type == 1"
      >
      </el-table-column>
      <el-table-column label="操作" width="180">
        <template slot-scope="{ row }">
          <el-button-group>
            <el-button
              type="primary"
              size="mini"
              :disabled="$button('763') == false ? true : false"
              @click="handleEdit(row)"
              >编辑</el-button
            >
            <el-button
              type="danger"
              size="mini"
              :disabled="$button('764') == false ? true : false"
              @click="handleDelete(row)"
              >删除</el-button
            >
          </el-button-group>
        </template>
      </el-table-column>
      <div slot="empty" class="emptyBg">
        <el-empty :image-size="200"></el-empty>
      </div>
    </el-table>
    <Pagination
      v-show="parseInt(pageObj.count) > 0"
      :total="parseInt(pageObj.count)"
      @change="getList"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.limit"
    />
  </div>
</template>
<script>
import {
  sysNoticeQuery,
  sysNoticeUpdate,
  sysNoticeDeleteById,
} from '@/api/admin/system/sysNotice.js'
import Bus from '@/utils/bus.js'
export default {
  data() {
    return {
      listQuery: {
        page: 1,
        limit: 10,
        type: this.$route.meta.type,
      },
      tableData: [],
      pageObj: {},
    }
  },
  watch: {
    $route(v) {
      this.listQuery.type = v.meta.type
      this.getList()
    },
  },
  mounted() {
    this.getList()
    Bus.$on('refreshNoticeList', (v) => {
      if (v) {
        Object.assign(this.listQuery, v)
      }
      this.getList()
    })
  },
  beforeDestroy() {
    Bus.$off('refreshNoticeList')
  },
  methods: {
    getList() {
      sysNoticeQuery(this.listQuery).then((res) => {
        this.tableData = res.data.data
        this.pageObj = res.data
      })
    },
    //编辑
    handleEdit(row) {
      Bus.$emit('showDialogNoticeAdd', row)
    },
    //发布
    handleStatus(v) {
      let { id, isShow } = v
      sysNoticeUpdate({ id, isShow }).then((res) => {
        this.getList()
        if (res.data.code != 0) return
        this.$message({
          message: '操作成功',
          type: 'success',
        })
      })
    },
    //删除确认
    handleDelete(row) {
      this.$confirm('确定要删除吗?', '提示', {
        confirmButtonText: '删除',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          this.delete(row)
        })
        .catch(() => {})
    },
    //删除
    delete(row) {
      sysNoticeDeleteById(row.id).then((res) => {
        this.getList()
        this.$message({
          message: '操作成功',
          type: 'success',
        })
      })
    },
  },
}
</script>